package com.zy.ivideo.repository;

import com.zy.ivideo.entity.SimpleVideoEntity;
import com.zy.ivideo.entity.unionentity.IUnionVideoSimpleEntity;
import org.hibernate.cache.spi.entry.StructuredCacheEntry;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 单类型视频信息数据仓库
 */
@Repository
public interface VideoSimpleRepository extends JpaRepository<SimpleVideoEntity,Long> {

    @Query(nativeQuery= true,value="select simple.*,author.name,author.description,author.avatar_url from b_simple_video simple,b_video_author author where simple.userid=author.user_id and simple.channelid=?1 limit ?2,?3")
    List<IUnionVideoSimpleEntity> getSimpleVideoByChannelId(String channelId,int page,int pagesize);

    @Query(nativeQuery= true,value="select simple.*,author.name,author.description,author.avatar_url from b_simple_video simple,b_video_author author where simple.userid=author.user_id limit ?1,?2")
    List<IUnionVideoSimpleEntity> getRecommendSimpleVideo(int page,int pagesize);

    /**
     * 修改点赞数
     * @param itemcode
     * @param num 传入1 或者 -1  1：点赞 -1 取消点赞
     * @return
     */
    @Transactional
    @Modifying(clearAutomatically=true)
    @Query(value="update b_simple_video set agreenum=agreenum+?1 where verifycode=?2",nativeQuery=true)
    int updateAgreeNum(int num,String itemcode);

    /**
     * 修改收藏数
     * @param num 传入1 或者 -1  1：收藏 -1 取消收藏
     * @param itemcode
     * @return
     */
    @Transactional
    @Modifying(clearAutomatically=true)
    @Query(value="update b_simple_video set collectionnum=collectionnum+?1 where verifycode=?2",nativeQuery=true)
    int updateCollectionNum(int num,String itemcode);

    /**
     * 增加播放数量
     * @param itemcode
     * @return
     */
    @Transactional
    @Modifying(clearAutomatically=true)
    @Query(value="update b_simple_video set playnum=playnum+1 where verifycode=?1",nativeQuery=true)
    int updatePlayNum(String itemcode);

    /**
     * 增加评论数量
     * @param itemcode
     * @return
     */
    @Transactional
    @Modifying(clearAutomatically=true)
    @Query(value="update b_simple_video set commentnum=commentnum+1 where verifycode=?1",nativeQuery=true)
    int updateCommentNum(String itemcode);
}
